package app1

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
import grails.gorm.transactions.Transactional
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.uri.UriTemplate

@Transactional
class SampleApiService {

    @HystrixCommand(commandKey = "SampleApiService#process(String)", fallbackMethod = "defaultProcess")
    Map process(String bearerToken, String dependency) {
        def addr = "http://localhost:8765", path = "/SERVICE-SET-1/sampleApi1?dependency={dependency}"

        def uri = UriTemplate.of(path).expand([dependency: dependency])
        HttpClient.create(new URL(addr)).toBlocking().exchange(
                HttpRequest.GET(uri)
                        .bearerAuth(bearerToken)
                        .header("Content-Type", "application/json")
                , Map).body()
    }

    Map defaultProcess(String bearerToken, String dependency) {
        [token: bearerToken, provider: "sampleApi1@serviceSet1", dependency: dependency, state: "offline"]
    }
}
